package com.quanshu.exchange.ui.gui.message.friends;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.UserInterface;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.support.utils.ToastHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 设置备注
 * Created by jye on 2017/12/14.
 */

public class SetRemarkActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.input_text)
    EditText inputText;
    @BindView(R.id.ok_btn)
    Button okBtn;
    private UserEntity userEntity;
    private String strName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_remark);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
    }

    private void initIntent() {
        userEntity = (UserEntity) getIntent().getSerializableExtra("UserEntity");
    }

    private void initView() {
        String name = AppUtils.getUserName(userEntity);
        if (!TextUtils.isEmpty(name)) {
            inputText.setText(name);
            inputText.setSelection(name.length());
        }
    }

    private void initListener() {
        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(okBtn, aVoid -> doSet());
        RxViewHelper.getInstance().afterTextChangeEvents(inputText,
                textViewAfterTextChangeEvent -> {
                    strName = textViewAfterTextChangeEvent.editable().toString();
                    inputText.setSelection(strName.length());
                });
    }

    private void doSet() {
        if (strName == null || strName.trim().length() < 1) {
            ToastHelper.toast("不能为空");
            return;
        }

        NetClient.getInstance()
                .getNetService(UserInterface.class)
                .setFriendRemark(AppUtils.getSignParams(currentUser.getMemberid(), userEntity.getMemberid(), strName), currentUser.getMemberid(), userEntity.getMemberid(), strName)
                .compose(new BaseTransformer<>(this))
                .subscribe(new BaseObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        userEntity.setRemark(strName);
                        MyApplication.getInstance().setCurrentUser(userEntity);
                        UserDao.getInstance().save(userEntity);
                        setResult(RESULT_OK);
                        finish();
                    }
                });
    }

}
